package com.vm.activity;

import org.json.JSONObject;

import com.android.volley.RequestQueue;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.vm.commons.CurrentUser;
import com.vm.commons.VMConst;
import com.vm.db.LocalUserInfoDao;
import com.vm.model.Goods;
import com.vm.model.LocalUserInfo;
import com.vm.model.Soft;
import com.vm.utils.JsonUtils;
import com.wwt.app.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年3月13日 下午4:51:45]
 * @Version: [v1.0]
 * 
 */
public class GoodsDetailActivity extends Activity {

	private Goods goods;
	ImageView iv_goodsImage;
	ImageView imgbtn_search_back;
	TextView tv_goodsName;
	Button btn_exchange;
	TextView tv_market_price;
	TextView tv_integral;
	RatingBar rb_star;
	TextView tv_goods_introduce;
	RequestQueue mQueue;
	ImageLoader mImageLoader;
//	private LocalUserInfoDao luidao;
//	private LocalUserInfo userInfo;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.goods_details);

//		init();
//
////		luidao = new LocalUserInfoDao(this);
////		userInfo = luidao.query();
//		mQueue = Volley.newRequestQueue(this);
//		mImageLoader = new ImageLoader(mQueue, new BitmapCache());
//
//		String goodsId = getIntent().getStringExtra("goodsId");
//		String url = VMConst.REQUEST_URL + "goodsdetail?goodsId=" + goodsId;
//		JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null,
//				new Listener<JSONObject>() {
//
//					@Override
//					public void onResponse(JSONObject arg0) {
//						goods = JsonUtils.jsonToBean(arg0, Goods.class);
//
//						tv_goodsName.setText(goods.getName());
//
//						tv_market_price.setText(Float.toString(goods.getPrice()));
//						tv_integral.setText(Integer.toString(goods
//								.getIntegral()));
//						tv_goods_introduce.setText(goods.getDetails());
//
//						ImageListener listener = ImageLoader.getImageListener(
//								iv_goodsImage,
//								android.R.drawable.ic_menu_rotate,
//								android.R.drawable.ic_delete);
//
//						mImageLoader.get(VMConst.URL + goods.getImageUrl(),
//								listener);
//					}
//				}, null);
//
//		mQueue.add(jsonObjectRequest);

	}

	/**
	 * 初始化
	 */
	public void init() {
//		iv_goodsImage = (ImageView) findViewById(R.id.iv_goodsImage);
//		imgbtn_search_back = (ImageView) findViewById(R.id.imgbtn_search_back);
//		tv_goodsName = (TextView) findViewById(R.id.tv_goodsName);
//		btn_exchange = (Button) findViewById(R.id.btn_exchange);
//		tv_market_price = (TextView) findViewById(R.id.tv_market_price);
//		tv_integral = (TextView) findViewById(R.id.tv_integral);
//		rb_star = (RatingBar) findViewById(R.id.rb_star);
//		tv_goods_introduce = (TextView) findViewById(R.id.tv_goods_introduce);
//		btn_exchange.setOnClickListener(btn_exchangeListener);
//
//		imgbtn_search_back.setOnClickListener(btn_backListener);
	}

	OnClickListener btn_exchangeListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// 确认是否有登录
			if (CurrentUser.USERID != null) {
				int goodsIntergral = Integer.parseInt(tv_integral.getText()
						.toString());
				if (CurrentUser.INTERGRAL > goodsIntergral) {

					Intent intent = new Intent(GoodsDetailActivity.this,
							OrderConfirmActivity.class);
					intent.putExtra("goods", goods);
//					intent.putExtra("userInfo", userInfo);
					startActivity(intent);
				} else {
					// 提示积分不足
					Toast toast = Toast.makeText(GoodsDetailActivity.this,
							"积分不足", Toast.LENGTH_SHORT);
					toast.show();
				}
			}
			else {
				//跳转到登录页面
				Intent intent = new Intent(GoodsDetailActivity.this,UserInfoActivity.class);
				startActivity(intent);
			}
		}
	};

	/**
	 * imgbtn_search_back 返回按钮监听
	 */
	OnClickListener btn_backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			onBackPressed();
		}
	};

	/**
	 * 手机自带返回按钮监听
	 */
	@Override
	public void onBackPressed() {
		super.onBackPressed();

	}
}
